﻿<?php
if(!defined('IN_PHPSCUP'))exit('hacking'); 

class RoleApp extends BaseApp
{
    function index()
    {

 		$TB_role = &$this->_model('role');  
		  
		$roledata = $TB_role->show();
		
        $this->assign('roledata', $roledata);		 
		 		     	
        $this->assign('role_file', $this->role_con(ROLE_CON_FILE));
					    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('role_list.view.html');
    }

    function edit()
    {
		
		$id = $_GET['id'];
		$TB_role = &$this->_model('role');	 
		
		$roledata = $TB_role->oneshow($id);	
	    $this->assign('roledata', $roledata);	
			    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('role_edit.view.html');
    }

    function add()
    {
		
	
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('role_add.view.html');
    }

    function power()
    {
		
	    $id = $_GET['id'];
		$pname = $_GET['v'];

		$TB_role = &$this->_model('role');
		$roledata = $TB_role->oneshow($id);
		$roledata['powerdb'] = @unserialize($roledata['powerdb']);
		
		unset($GLOBALS['roledata_powerdb']);
		$GLOBALS['roledata_powerdb'] = $roledata['powerdb'];

        $this->assign('rolename', $roledata['role_name']); 
		
        $this->assign('pname', $pname); 	
        $this->assign('powerid', $id);     
        $this->assign('powerdata', $this->get_powerdata($pname));
					    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('power_list.view.html');
    }

    function powerpost()
    {
		if(!isset($_POST['id']))
		{
		    $_POST['id']='';
		}
		if(!isset($_POST['power']))
		{
		    $_POST['power']='';
		}		
		
		if(!isset($_POST['v']))
		{
		    $_POST['v']='';
		}		
		
	    $id = $_POST['id'];
        $power = $_POST['power'];
		$pname = $_POST['v'];
		
		$powerdata = array();
		

		if(empty($power))
		{
		  $power = array();
		}

		foreach($power as $key => $value)
		{
		  $str_array = explode('|',$value);
		  $key1 = $str_array[0];
		  $value1 =  explode(':',$str_array[1]);
		      foreach($value1 as $key2 => $value2)
			  {
			      if($value2)
				  {
				   $powerdata[$key1][$value2] = array('set' => 1,'url'=>''); 
				  }
			      
			  }		  
		}

/*       $powerdata_temp = array();

        $powerdata_temp = $this->get_powerdata($pname);
  
		foreach($powerdata_temp as $key => $value)
		{

              
		      foreach($value['act'] as $key2 => $value2)
			  {
			  
			  		$temp_array =  explode(':',$key2);
		            foreach($temp_array as $key3 => $value3)
			        {
			              if($value3&&(!isset($powerdata[$value['app']][$value3])||!isset($powerdata[$value['app']][$value3]['set'])))
				          {
				           $powerdata[$value['app']][$value3] = array('set' => 0,'url'=>''); 
				          }
			      
			        }		 

			      
			  }		  
		}
*/


		 $TB_role = &$this->_model('role');
		 $roledata = $TB_role->oneshow($id);
		 $roledata['powerdb'] = @unserialize($roledata['powerdb']);
	 
		 $roledata['powerdb'][$pname] = $powerdata;
	 
		 $roledata['powerdb'] = serialize($roledata['powerdb']);

         $TB_role->editpower($id,$roledata['powerdb']);
		 $this->write_cache();		 
		 $this->index();		 


    }



    function addpost()
    {

	     $data = $_POST['data'];
		 	 
		 $TB_role = &$this->_model('role');
		 $icon = $TB_role->check($data['name']);

		 if(!$icon['role_id'])
		 {
		 
		 $TB_role->post($data);		 
		// $this->write_cache();
		 $this->index();		 
		 
		 }
		 else
		 {
		 $notice = &get_message(array('msg'=>'False!','url'=>'','title'=>'Notice'));
		 $notice ->display();
		 } 	 
		


    }

    function editpost()
    {

	     $data = $_POST['data'];

		 
		 $id = intval($data['id']);	 
		 
		 $TB_role = &$this->_model('role');
		 $icon = $TB_role->check($data['name']);

		 if(!$icon['role_id'])
		 {
		 
		
		 $TB_role->editaction($id,$data);
		 
		// $this->write_cache();
		 $this->index();	 
		 
		 }
		 else
		 {
		 $notice = &get_message(array('msg'=>'False!','url'=>'','title'=>'Notice'));
		 $notice ->display();
		 } 	 		 


		 

    }



    function lname()
    {
		 $data = array();
		 
	     $id = $_GET['id'];
		 $data['name'] = $_GET['v'];
		 
		 $TB_role = &$this->_model('role'); 	 		
		 $TB_role->editname($id,$data); 
		// $this->write_cache();		 
		 $this->index();			  

    }
	


    function del()
    {

	     $id = $_GET['id'];
		 $TB_role = &$this->_model('role');
			 
		 $TB_role->del($id);	
		 	 
		// $this->write_cache();		 
		 $this->index();	  

    }

    function get_powerdata($name)
    {
         

		$filename = ROOT_PATH . '/lib/power/'.$name.'.power.php'; 			 

		if(is_file($filename))
	    {
	    $powerdata = include($filename); 
		}
		else
	    {
        exit('Missing PowerFile');
		}  		 
        return $powerdata;

    }	


    function cache()
    {
         
		 $this->write_cache();		 
		 $this->index();		 


    }	

    function write_cache()
    {
	
		$TB_role = &$this->_model('role');			
		$roledata = $TB_role->show();
		
		$roledata_temp = array();
		
	    $filebag = $this->role_con(ROLE_CON_FILE);
		foreach($roledata as $key => $value)
		{		 		 
		      $value['powerdb'] = @unserialize($value['powerdb']);
		 
		      
		 
		      foreach($filebag as $key1 => $value1)
		      {	
			   $roledata_temp[$value1][$value['role_id']]['role_name'] = $value['role_name'];
			   if(isset($value['config'][$value1]))
			   {
			   $roledata_temp[$value1][$value['role_id']]['config'] = $value['config'][$value1];			   
			   }
			   else
			   {
			   $roledata_temp[$value1][$value['role_id']]['config'] = '';			   
			   }
                if(isset($value['powerdb'][$value1]))
				{
		       $roledata_temp[$value1][$value['role_id']]['powerdb'] = $value['powerdb'][$value1];					
				}
				else
				{
				$roledata_temp[$value1][$value['role_id']]['powerdb'] = '';
				}
		  	     
		      }
		}

		foreach($filebag as $key1 => $value1)
		{	
		     $cache_filename = ROOT_PATH . '/data/power/'.$value1.'.power.php';
		
	         write_file($cache_filename, "<?php if(!defined('IN_PHPSCUP'))exit('hacking');  \nreturn " . var_export($roledata_temp[$value1] , true) . ";\n?>");
		
		     if(!is_writable($cache_filename))
		     {
		     exit('file directory is not writed power');//文件目录权限不可写
	         }			  	     
	    }		
		

         
		

    }

    function role_con($str)
    {
	      $array = array();
          $str_array = explode('|',$str);
	      foreach($str_array as $key => $value)
	      {
	            if($value)
		        {
			   
			   $array[$key] = $value;
		   
		        }
	 
	      }    
	   
	      return $array; 		

    }

  
}

function definerole($id)
{
        if(empty($id)) 
		{
		return false;
		}
		if(str_explode($id,ROLE_Y_ID)||str_explode($id,ROLE_P_ID) ||str_explode($id,ROLE_V_ID))
		{
		
		return true;
		}
		
        return false;

}


function str_explode($id,$str)
{

       $array = array(); 
       $str_array = explode('|',$str);
	   foreach($str_array as $key => $value)
	   {
	         if($value)
		     {			 
		          $array =  explode(':',$value);
			 	  if(in_array($id,$array))
				  {
				  return true;
				  }	
				  unset($array);	 
		     }
	 
	   } 

        return false;

}




function powercheck($app,$act,$scup)
{

		$str_array =  explode(':',$act);
		
		foreach($str_array as $key => $value)
		{         
		          if(!isset($GLOBALS['roledata_powerdb'][$scup][$app][$value]['set']))
				  {
				  $GLOBALS['roledata_powerdb'][$scup][$app][$value]['set']='';
				  }
			      if($value && $GLOBALS['roledata_powerdb'][$scup][$app][$value]['set']==1)
				  {
                   	return true;			   
				  }
			      
		}		  

        return false;

}




?>